<?php

declare(strict_types=1);

namespace App\Services;

use Hyperf\Di\Annotation\Inject;
use App\Exception\ApiException;
use App\Constants\ErrorCode;
use App\Model\News;
use App\Model\Activity;
use App\Model\PointsRanking;

class IndexService extends BaseService
{

    public function getNews()
    {
        $news = News::query()->select('id','title','category','created_at')->limit(6)->orderBy('id', 'DESC')->get()->toArray();
        foreach($news as $key=>$val)
        {
            $news[$key]['date'] = date('m-d', strtotime($val['created_at']));
        }

        return $news;
    }

    public function getActivity()
    {
        $activity = Activity::query()->select('id','title','image','begin_time','end_time','is_new')->get()->toArray();
        foreach($activity as $key=>$val)
        {
            $activity[$key]['status'] = check_time_status($val['begin_time'], $val['end_time']);
        }

        return $activity;
    }

    public function getMonthPointsRanking()
    {
        $currentMonth = date('m');
        $lastMonth = $currentMonth - 1;
        $pointsRanking = PointsRanking::query()->with('user')->select('id','user_id','month','points')->where('month', $lastMonth)->orderBy('points', 'DESC')->get()->toArray();

        return $pointsRanking;
    }

    public function getGoods()
    {
        $category = [User::OFF_STATUS, User::GAME_PACK, User::PERIPHERAL];
        $limit = 6;

        $result = [];

        foreach ($category as $val) {
            $goods = Goods::query()->select('id','category','title','image','price','num')->where('category', $val)->limit($limit)->get()->toArray();
            $result[$val] = $goods;
        }

        return $result;
    }

}